﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using StudentManage.Data;
using StudentManage.Common;

namespace StudentManage.Web.Admin.Management.SysManage
{
    public partial class StudentList : SecurityPage
    {
        public override string PrivCode()
        {
            return "ae49a782978c8de5";
        }

        private void TVBangding()
        {
            this.TV_Student.Nodes.Clear();
            List<xy_bmxbm> list = xy_bmxbm.All().ToList();
            List<DataAssociate> assXBAndBJ = DataAssociate.Where("TypeId=@0", Convert.ToInt32(EnumData.AssociateType.系别与班级)).ToList();
            List<xy_bj> bjlist = xy_bj.All().ToList();
            list.Where(p => p.BMFQID == "03").ToList().ForEach(o =>
                {
                    //学院
                    TreeNode xynode = new TreeNode();
                    xynode.Text = o.BMMC;
                    xynode.Value = o.BMDM;
                    xynode.Target = Convert.ToInt32(EnumData.TvNodeType.学院).ToString();
                    list.Where(k => k.BMFQID == o.BMDM && k.BMMC.Contains("系")).ToList().ForEach(j =>
                    {
                        //系别
                        TreeNode xbnode = new TreeNode();
                        xbnode.Text = j.BMMC;
                        xbnode.Value = j.BMDM;
                        xbnode.Target = Convert.ToInt32(EnumData.TvNodeType.系).ToString();

                        var s_XBAndBJ = from B in assXBAndBJ where B.ParentNo == j.BMDM select B.ChildNo;
                        var q_BJ = from A in bjlist
                                   where s_XBAndBJ.Contains(A.BJDM)
                                   select A;
                        List<xy_bj> childbjlist = q_BJ.ToList();
                        childbjlist.ForEach(h =>
                        {
                            //班级
                            TreeNode bjnode = new TreeNode();
                            bjnode.Text = h.BJMC;
                            bjnode.Value = h.BJDM;
                            bjnode.Target = Convert.ToInt32(EnumData.TvNodeType.班级).ToString();
                            xbnode.ChildNodes.Add(bjnode);
                        });
                        xynode.ChildNodes.Add(xbnode);
                    });
                    this.TV_Student.Nodes.Add(xynode);
                });
            this.TV_Student.CollapseAll();
        }

        private void RepBanding()
        {
            if (this.TV_Student.SelectedNode == null) return;

            var s = from A in this.TV_Student.SelectedNode.AllNodes() 
                    where A.Target==Convert.ToInt32(EnumData.TvNodeType.班级).ToString()
                    select A.Value;

            var q = from B in LinqUtility.LinqContext.xy_xsxx 
                    where s.Contains(B.BJDM)
                    select B;

            int RowCount = q.Count();
            this.AspNetPager1.RecordCount = RowCount;
            int PageSize = this.AspNetPager1.PageSize;
            int SkipCount = PageSize * (this.AspNetPager1.CurrentPageIndex - 1);

            List<xy_xsxx> list = q.Skip(SkipCount).Take(PageSize).ToList();
            this.Rep_DataList.DataSource = list;
            this.Rep_DataList.DataBind();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack) return;
            RepBanding();
            TVBangding();
        }

        protected void Rep_DataList_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            string XH = Convert.ToString(e.CommandArgument);
            if (e.CommandName == "Del")
            {
                xy_xsxx.Delete("XH=@0", XH);
                UserList.Delete("XH=@0 and IsStudent=@1", XH, true);
                RepBanding();
            }
        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            this.RepBanding();
        }

        protected void TV_Student_SelectedNodeChanged(object sender, EventArgs e)
        {
            RepBanding();
        }

        protected void RefreshList_Click(object sender, EventArgs e)
        {
            this.RepBanding();
        }
    }
}